#ifndef READCARD_H
#define READCARD_H

#include "sys.h"
#include <stm32f4xx.h>

// READCARD数据包
#define READCARD_MAX 11      // 最大
#define READCARD_TAIL 0x03   // 尾部
#define READCARD_HEADER 0x02 // 头部
#define READCARD_LENGTH 0x09 // 长度

// READCARD引脚
#define READCARD_RX GPIO_Pin_6 // RX

// USART引脚
#define READCARD_USART_RX GPIO_PinSource6 // RX

// GPIO
#define READCARD_GPIO GPIOD                                                                  // GPIOD组
#define READCARD_GPIO_Pin READCARD_RX                                                        // RCARD脚
#define READCARD_RCC_APBPeriphClockCmd RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE) // APB时钟
#define READCARD_RCC_AHBPeriphClockCmd RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE)  // AHB时钟

// USART
#define READCARD_USART USART2                 // USART2组
#define READCARD_USART_BaudRate 9600          // USART波特
#define READCARD_GPIO_AF_USART GPIO_AF_USART2 // 复用USART

// NVIC
#define READCARD_USART_IRQn USART2_IRQn              // USART中断
#define READCARD_NVIC_IRQChannelSubPriority 3        // 响应优先级
#define READCARD_NVIC_IRQChannelPreemptionPriority 3 // 抢占优先级
#define READCARD_USART_IRQHandler USART2_IRQHandler  // 中断处理器

/**
 * @name 卡片信息
 * @struct
 */
#pragma pack(1)
typedef struct tagREADCARD_CARD {
    uint8_t cardType; // 卡片类型
    uint32_t cardID;  // 卡片ID
} READCARD_CARD, *PREADCARD_CARD;
#pragma pack()

/**
 * @name readCard初始化
 * @description
 * @function
 * @returns {void}
 * @requires []
 */
void readCard_init(void);

/**
 * @name 接收数据
 * @description
 * @function
 * @returns {READCARD_CARD} 数据
 * @requires []
 */
READCARD_CARD readCard_recv(void);
#endif
